package com.example.farmeri.ui.base;

import android.os.Bundle;
import android.os.PersistableBundle;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.farmeri.R;
import com.example.farmeri.adapter.MyItemDecoration;
import com.example.farmeri.presenter.base.BasePresenter;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.lang.reflect.Method;

/**
 * 线性RecycleView的基类
 * Create by weiqishan on 2020/4/9.
 */
public abstract class BaseLinearRecycleViewActivity<K extends BasePresenter> extends BaseActivity<K> {

    protected BottomSheetDialog bottomSheetDialog;    //单选删除弹出框 (底部模态弹出框)
    protected View dialogView;    //单选删除弹出框视图
    protected BottomSheetBehavior bottomSheetBehavior;    //多选弹出框 (底部弹出框)
    protected RecyclerView recyclerView;



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    }


    /**
     * 使用ToolBar
     * @param layout toolBar的地址
     * @param mTitle    主标题
     * @param isCenter  主标题是否居中
     */
    protected void initToolBar(int layout, String mTitle, Boolean isCenter) {
        Toolbar mToolbar = findViewById(layout);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        mToolbar.setTitle(mTitle);
        mToolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.colorWhite));
        mToolbar.setNavigationContentDescription("返回");
        mToolbar.setNavigationIcon(R.drawable.ic_chevron_left_white_24dp);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (isCenter){
            //设置标题居中
            TextView title = (TextView) mToolbar.getChildAt(0);
            title.getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;
            title.setGravity(Gravity.CENTER_HORIZONTAL);
        }

    }

    /**
     * 让文字与图标一同显示
     * @param featureId
     * @param menu
     * @return
     */
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return super.onMenuOpened(featureId, menu);
    }

    /**
     * 初始化底部模态弹出框
     * @param layout 布局位置
     */
    protected void initBottomSheetDialog(Integer layout) {
        dialogView = getLayoutInflater().inflate(layout,null);
        bottomSheetDialog = new BottomSheetDialog(this);
        bottomSheetDialog.setContentView(dialogView);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.setCancelable(true);
    }

    protected void initBottomSheetDialog(Integer layout,@Nullable String hint) {
        dialogView = getLayoutInflater().inflate(layout,null);
        bottomSheetDialog = new BottomSheetDialog(this);
        bottomSheetDialog.setContentView(dialogView);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.setCancelable(true);
        if (hint!=null){
            TextView textView = dialogView.findViewById(R.id.dialog_bottom_hint);
            textView.setText(hint);
        }
    }

    /**
     * 初始化底部弹出框
     * @param layout
     */
    protected void initBottomBehavior(Integer layout) {
        View view2 = findViewById(layout);
        bottomSheetBehavior = BottomSheetBehavior.from(view2);
    }

    /**
     * 获取屏幕宽度
     * @return
     */
    protected int getScreeWidth() {
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 为线性列表填充数据
     */
    protected void linearRecyclerViewAddData(int layout_recycleView,RecyclerView.Adapter mAdapter,int verticalSpaceHeight) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView = findViewById(layout_recycleView);
//        recyclerView.addItemDecoration(new MyItemDecoration(verticalSpaceHeight));
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(mAdapter);
    }




}
